﻿using System;
using System.Collections.Generic;
using System.Linq;

public partial class LaneControl : System.Web.UI.UserControl
{
    public string Name { get; set; }
    public string WipLimit { get; set; }
    public string WipFloor { get; set; }

    public List<WorkItemView> InProgressItems { get; set; }
    public List<WorkItemView> QueueItems { get; set; }

    public string LeftHeader
    {
        set { LeftLaneHeader.Text = value; }
    }

    public string RightHeader
    {
        set { RightLaneHeader.Text = value; }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        LaneTitleLabel.Text = Name;

        int inProgressCount = InProgressItems == null ? 0 : InProgressItems.Count();
        int queueCount = QueueItems == null ? 0 : QueueItems.Count();
        
        //
        // If the lane has a WIP limit, show it in the header and change color as required.
        //
        if (WipLimit != "*")
        {
            // If the lane has a WIP limit, show it in the lane header
            WipLimitLabel.Text = string.Format("({0})",WipLimit);

            // If WIP limit is exceeded change the css style of the lane
            if (inProgressCount + queueCount > Convert.ToInt32(WipLimit))
            {
                LanePanel.CssClass = "laneWipExceed";
            }
            else
            {
                LanePanel.CssClass = "lane";
            }
        }
        if (!String.IsNullOrWhiteSpace(WipFloor))
        {
            if (inProgressCount + queueCount < Convert.ToInt32(WipFloor))
                LanePanel.CssClass = "laneWipLow";
        }

        // Show the left or right columns if they are enabled
        LeftLanePanel.Visible = LeftLaneHeader.Visible = InProgressItems != null;
        RightLanePanel.Visible = RightLaneHeader.Visible = QueueItems != null;

        CenterDividor.Visible = LeftLanePanel.Visible && RightLanePanel.Visible;

        // Bind the item data
        InProgressList.DataSource = InProgressItems;
        QueueList.DataSource = QueueItems;

        DataBind();
    }
}